//
//  RecommendViewController.m
//  A Learning Walk
//
//  Created by dllo on 16/6/13.
//  Copyright © 2016年 战斗民族. All rights reserved.
//

#import "RecommendViewController.h"
#import "newestViewController.h"
#import "expressViewController.h"
#import "videoViewController.h"
#import "newsViewController.h"
#import "carViewController.h"
static CGFloat const titleH = 44;
static CGFloat const navBarH = 0;
static CGFloat const maxTitleScale = 1.3;
#define WSColor(r, g, b) [UIColor colorWithRed:(r)/255.0f green:(g)/255.0f blue:(b)/255.0f alpha:1.0f]
#define ScreenW [UIScreen mainScreen].bounds.size.width
#define ScreenH [UIScreen mainScreen].bounds.size.height
@interface RecommendViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *titleScrollView;
@property (nonatomic, strong) UIScrollView *contentScrollView;
// 选中按钮
@property (nonatomic, strong) UIButton *selTitleButton;
@property (nonatomic, strong) NSMutableArray *buttons;

@property (nonatomic, strong) UILabel *navLabel;
@end

@implementation RecommendViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setAutomaticallyAdjustsScrollViewInsets:YES];
    self.navigationController.navigationBar.translucent = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"边走边听";
     [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor orangeColor],NSForegroundColorAttributeName,[UIFont systemFontOfSize:17],NSFontAttributeName, nil]];
    
    /*
    //搜索栏
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];//allocate titleView
    // UIColor *color =  [UIColor clearColor];
    titleView.backgroundColor = [UIColor clearColor];
    
   //Set to titleView
    self.navigationItem.titleView = titleView;

    UISearchBar *searchBar = [[UISearchBar alloc] init];
    searchBar.delegate = self;
    [titleView addSubview:searchBar];
    searchBar.barStyle = UIBarStyleDefault;
    searchBar.placeholder = @"搜索论坛或者帖子标题";
    [searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    [searchBar setBackgroundImage:[[UIImage alloc] init]];//去掉搜索框的边界黑线
    searchBar.barTintColor = WSColor(120, 163, 200);
    searchBar.layer.cornerRadius = 18;
    //searchBar.tintColor = [UIColor clearColor];
    searchBar.layer.masksToBounds = YES;
    //searchBar.showsCancelButton = YES;
    searchBar.showsBookmarkButton = YES;
    */
    
 
    //滑动栏
    self.buttons = [NSMutableArray array];
    // 1.创建小scrollview
    [self setupTitleScrollView];
    
    
    // 2.创建大scrollview
    [self setupContentScrollView];
    // 3.给大scrollview设置内容vc
    [self addChildViewController];
    
    // 4.布局按钮标题
    [self setupTitle];
    
    self.contentScrollView.contentSize = CGSizeMake(self.childViewControllers.count * ScreenW, 0);
    self.contentScrollView.pagingEnabled = YES;
    self.contentScrollView.showsHorizontalScrollIndicator = NO;
    self.contentScrollView.delegate = self;
    self.contentScrollView.bounces = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
}



// 设置头部标题
- (void)setupTitleScrollView
{
    // 判断是否存在导航控制器来判断y值
    CGFloat y = self.navigationController ? navBarH : 0;
    CGRect rect = CGRectMake(0, y, ScreenW, titleH);
    
    UIScrollView *titleScrollView = [[UIScrollView alloc] initWithFrame:rect];
    titleScrollView.dk_backgroundColorPicker = DKColorPickerWithRGB(0xF8F8FF,0xDCDCDC);
    [self.view addSubview:titleScrollView];
    
    self.titleScrollView = titleScrollView;
    
}


#pragma mark - 设置内容
- (void)setupContentScrollView{
    CGFloat y = CGRectGetMaxY(self.titleScrollView.frame);
    CGRect rect = CGRectMake(0, y, ScreenW, ScreenH - navBarH);
    
    UIScrollView *contentScrollView = [[UIScrollView alloc] initWithFrame:rect];
    [self.view addSubview:contentScrollView];
    
    self.contentScrollView = contentScrollView;
    
    
}

#pragma mark - 添加子控制器
- (void)addChildViewController
{
    newestViewController *vc = [[newestViewController alloc] init];
    vc.title = @"头条";
    [self addChildViewController:vc];
    
    expressViewController *vc1 = [[expressViewController alloc] init];
    vc1.title = @"热点";
    [self addChildViewController:vc1];
    
    videoViewController *vc2 = [[videoViewController alloc] init];
    vc2.title = @"视频";
    [self addChildViewController:vc2];
    
    newsViewController *vc3 = [[newsViewController alloc] init];
    vc3.title = @"新闻";
    [self addChildViewController:vc3];
    
   carViewController *vc4 = [[carViewController alloc] init];
    vc4.title = @"用车";
    [self addChildViewController:vc4];
    
}

// 布局按钮坐标
- (void)setupTitle
{
    NSUInteger count = self.childViewControllers.count;
    
    CGFloat x = 0;
    CGFloat w = ScreenW / 5;
    CGFloat h = titleH - 3;
    
    for (int i = 0; i < count; i++)
    {
        UIViewController *vc = self.childViewControllers[i];
        
        x = i * w;
        CGRect rect = CGRectMake(x, 0, w, h);
        UIButton *btn = [[UIButton alloc] initWithFrame:rect];
        
        btn.tag = i;
        [btn setTitle:vc.title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        
        [btn addTarget:self action:@selector(chick:) forControlEvents:UIControlEventTouchDown];
        
        [self.buttons addObject:btn];
        [self.titleScrollView addSubview:btn];
        
        // 设置默认第一个被选中
        if (i == 0)
        {
            [self chick:btn];
        }
        
    }
    self.titleScrollView.contentSize = CGSizeMake(count * w, 0);
    self.titleScrollView.showsHorizontalScrollIndicator = NO;
}

// 按钮点击
- (void)chick:(UIButton *)btn
{
    CATransition* animation = [CATransition animation];
    [animation setDuration:0.5f];
    animation.type = @"rippleEffect";
    animation.subtype=kCATransitionFromBottom;
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
    [btn.layer addAnimation:animation forKey:@"switchView"];
    
    
    
    // 设置标题颜色为选中颜色
    [self selTitleBtn:btn];
    NSUInteger i = btn.tag;
    CGFloat x = i * ScreenW;
    [self setUpOneChildViewController:i];
    // 点击按钮切换对应的vc
    self.contentScrollView.contentOffset = CGPointMake(x, 0);
}

// 选中按钮
- (void)selTitleBtn:(UIButton *)btn
{
    // 先还原上次选中按钮颜色和大小
    [self.selTitleButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    self.selTitleButton.transform = CGAffineTransformIdentity;
    // 设置当前选中的
    [btn setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    btn.transform = CGAffineTransformMakeScale(maxTitleScale, maxTitleScale);
    
    self.selTitleButton = btn;
    
    // 设置选中按钮保持在中心点附近位置
    [self setupTitleCenter:btn];
}

- (void)setupTitleCenter:(UIButton *)btn
{
    CGFloat offset = btn.center.x - ScreenW * 0.5;
    
    // 如果偏移量小于0,说明按钮的中心点是在屏幕中心点的左侧,所以scrollview就不用偏移,保持偏移量为0就行.
    // 如果按钮中心点大于屏幕中心点,那么scrollview就偏移二者的差距就行.
    if (offset < 0)
    {
        offset = 0;
    }
    
    // scrollview的内容宽度-屏幕宽度,就是最大偏移量.也就是scrollview偏移最大偏移量就能显示最后的按钮了.
    CGFloat maxOffset = self.titleScrollView.contentSize.width - ScreenW;
    if (offset > maxOffset)
    {
        offset = maxOffset;
    }
    
    [self.titleScrollView setContentOffset:CGPointMake(offset, 0) animated:YES];
    
    
}


//滑动到某个位置就加载相应的位置的vc,不多余创建
- (void)setUpOneChildViewController:(NSUInteger)i
{
    CGFloat x = i * ScreenW;
    
    UIViewController *vc = self.childViewControllers[i];
    
    //如果当前vc上有子视图就不再添加
    if (vc.view.superview) {
        return;
    }
    // 只是将vc的view加在大scrollview上
    vc.view.frame = CGRectMake(x, 0, ScreenW, ScreenH - self.contentScrollView.frame.origin.y);
    
    [self.contentScrollView addSubview:vc.view];
    
}
#pragma mark - UIScrollViewDelegate
// 滑动scrollview 改变按钮和vc
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    // 滑动scrollview得到当前下标
    NSUInteger i = self.contentScrollView.contentOffset.x / ScreenW;
    // 更新选中按钮的颜色和大小
    [self selTitleBtn:self.buttons[i]];
    // 切换vc
    [self setUpOneChildViewController:i];
}



// 只要滚动UIScrollView就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
     // 主要实现滑动中的缩放效果
    CGFloat offsetX = scrollView.contentOffset.x;
    // 左边按钮就是当前选中按钮
    NSInteger leftIndex = offsetX / ScreenW;
    NSInteger rightIndex = leftIndex + 1;
    
    //    NSLog(@"%zd,%zd",leftIndex,rightIndex);
    
    UIButton *leftButton = self.buttons[leftIndex];
    
    UIButton *rightButton = nil;
    if (rightIndex < self.buttons.count) {
        rightButton = self.buttons[rightIndex];
    }
    
    // offsetX / ScreenW = 当前下标左右的范围值 比如当前leftIndex为2 那么offsetX / ScreenW就是1.1~2.9的范围内  所以scaleR比例永远为0~0.9
    CGFloat scaleR = offsetX / ScreenW - leftIndex;
    // scaleL的值就为0.9~0
    CGFloat scaleL = 1 - scaleR;
    
    
    // 设置所有按钮的缩放范围都是1~1.3
    CGFloat transScale = maxTitleScale - 1;
    // 0~1范围的数*0.3永远是0~0.3的范围
    leftButton.transform = CGAffineTransformMakeScale(scaleL * transScale + 1, scaleL * transScale + 1);
    
    rightButton.transform = CGAffineTransformMakeScale(scaleR * transScale + 1, scaleR * transScale + 1);
    //NSLog(@"L== %f  R== %f",scaleL  ,scaleR);
    
    
    UIColor *rightColor = [UIColor colorWithRed:scaleR green:0 blue:0 alpha:1];
    UIColor *leftColor = [UIColor colorWithRed:scaleL green:0 blue:0 alpha:1];
    
    [leftButton setTitleColor:leftColor forState:UIControlStateNormal];
    [rightButton setTitleColor:rightColor forState:UIControlStateNormal];
}


@end
